Security News
GitHub Removes Malicious Pull Requests Targeting Open Source Repositories
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
cloneable-readable
Advanced tools
The cloneable-readable npm package allows you to create cloneable versions of Node.js readable streams. This means you can create multiple independent readers from a single readable stream, which can be useful in scenarios where you need to process the same stream data in different ways simultaneously.
Cloning a Readable Stream
This feature allows you to clone a readable stream. The code sample demonstrates how to create a cloneable version of a readable stream and then read data from both the original and cloned streams.
const cloneable = require('cloneable-readable');
const { Readable } = require('stream');
const originalStream = new Readable({
read() {
this.push('data');
this.push(null);
}
});
const clonedStream = cloneable(originalStream);
clonedStream.on('data', (chunk) => {
console.log(`Cloned stream data: ${chunk}`);
});
originalStream.on('data', (chunk) => {
console.log(`Original stream data: ${chunk}`);
});
Multiple Clones
This feature allows you to create multiple clones of a readable stream. The code sample demonstrates how to create two clones from the original stream and read data from both clones.
const cloneable = require('cloneable-readable');
const { Readable } = require('stream');
const originalStream = new Readable({
read() {
this.push('data');
this.push(null);
}
});
const clonedStream1 = cloneable(originalStream);
const clonedStream2 = clonedStream1.clone();
clonedStream1.on('data', (chunk) => {
console.log(`Cloned stream 1 data: ${chunk}`);
});
clonedStream2.on('data', (chunk) => {
console.log(`Cloned stream 2 data: ${chunk}`);
});
The multistream package allows you to combine multiple streams into a single readable stream. While it does not provide cloning functionality, it is useful for merging streams together, which can be an alternative approach depending on the use case.
The through2 package is a tiny wrapper around Node.js streams2 Transform to avoid explicit subclassing noise. While it does not provide cloning functionality, it is useful for creating transform streams that can process data in a pipeline.
Clone a Readable stream, safely.
'use strict'
var cloneable = require('cloneable-readable')
var fs = require('fs')
var pump = require('pump')
var stream = cloneable(fs.createReadStream('./package.json'))
pump(stream.clone(), fs.createWriteStream('./out1'))
// simulate some asynchronicity
setImmediate(function () {
pump(stream, fs.createWriteStream('./out2'))
})
cloneable-readable automatically handles objectMode: true
.
This module comes out of an healthy discussion on the 'right' way to clone a Readable in https://github.com/gulpjs/vinyl/issues/85 and https://github.com/nodejs/readable-stream/issues/202. This is my take.
YOU MUST PIPE ALL CLONES TO START THE FLOW
You can also attach 'data'
and 'readable'
events to them.
Create a Cloneable
stream.
A Cloneable has a clone()
method to create more clones.
All clones must be resumed/piped to start the flow.
Check if stream
needs to be wrapped in a Cloneable
or not.
This project was kindly sponsored by nearForm.
MIT
FAQs
Clone a Readable stream, safely
The npm package cloneable-readable receives a total of 1,301,749 weekly downloads. As such, cloneable-readable popularity was classified as popular.
We found that cloneable-readable demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.